package com.example.newinfinity.Account.MyWorks

import android.os.Build
import androidx.annotation.RequiresApi
import androidx.fragment.app.activityViewModels
import androidx.recyclerview.widget.PagerSnapHelper
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide
import com.drake.brv.utils.bindingAdapter
import com.drake.brv.utils.linear
import com.drake.brv.utils.models
import com.drake.brv.utils.setup
import com.example.newinfinity.BaseFragment
import com.example.newinfinity.MyViewModel.PhotoBrowserViewModel
import com.example.newinfinity.R
import com.example.newinfinity.databinding.FragmentPhotoBroswerBinding
import com.example.newinfinity.databinding.PhotoBroswerItemBinding

class PhotoBrowserFragment : BaseFragment<FragmentPhotoBroswerBinding>() {
    private val viewModel: PhotoBrowserViewModel by activityViewModels()

    override fun initViewBinding(): FragmentPhotoBroswerBinding {
        return FragmentPhotoBroswerBinding.inflate(layoutInflater)
    }

    @RequiresApi(Build.VERSION_CODES.Q)
    override fun initView() {
        super.initView()

        viewModel.imageModels.observe(this) { model ->
            mBinding.recyclerView.models = model
            mBinding.recyclerView.bindingAdapter.notifyDataSetChanged()
        }

        mBinding.recyclerView.linear(RecyclerView.HORIZONTAL).setup {
            addType<WorksModel>(R.layout.photo_broswer_item)
            onBind {
                val binding = getBinding<PhotoBroswerItemBinding>()
                val model = getModel<WorksModel>()

                //使用glide加载图片
                val blurImage = BlurImageUtil.blurImageFromPath(model.thumbnailPath, 30f)
                binding.blurImageView.setImageBitmap(blurImage)
                Glide.with(requireContext()).load(model.thumbnailPath).into(binding.mainImageView)
            }
        }

        PagerSnapHelper().attachToRecyclerView(mBinding.recyclerView)
    }
}